﻿using System;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using BrainTechLLC.Silverlight.Controls;

namespace BrainTechLLC.Silverlight.ClientApplication
{
    public partial class EditCommandToPerform : BaseDataVisualizer, ISingleItemDataVisualizer
    {
        public override void SetVisualizedData(DataVisualizer vis, object o) { Command = o as CommandToPerform; base.SetVisualizedData(vis, o); }
        public Type VisualizeType { get { return typeof(CommandToPerform); } }
        public string VisualizerName { get { return "EditCommandToPerform"; } }

        public static readonly DependencyProperty CommandProperty = DependencyProperty.Register(
            "Command", typeof(CommandToPerform), typeof(EditCommandToPerform), new PropertyMetadata(null, new PropertyChangedCallback(CommandChanged)));

        public static void HandleChange<T2>(DependencyObject d, DependencyPropertyChangedEventArgs e, Action<EditCommandToPerform, T2> action)
        {
            d.HandleChange<EditCommandToPerform, T2>(e, action, (t) => t.UpdateBindings());
        }

        public CommandToPerform Command
        {
            get { return (CommandToPerform)GetValue(CommandProperty); }
            set { SetValue(CommandProperty, value); }
        }

        private static void CommandChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            HandleChange<CommandToPerform>(d, e, (t, val) => t.Command = val);            
        }

        public override void UpdateBindings()
        {
            if (gridData != null)
                gridData.DataContext = Command;

        }

        public override string TitleText
        {
            get
            {
                if (Command == null || Command.PerformMethod == null)
                    return string.Empty;

                return string.Format("Command {0}, Check {1}",
                    Command.JoinTypeAndMethodName(Command.PerformMethod),
                    Command.JoinTypeAndMethodName(Command.CheckPermittedMethod));
            }
        }

        public EditCommandToPerform()
        {
            InitializeComponent();
        }
    }
}